<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串和数值类型相互转换</title>
    
</head>
<body>
    <script type="text/javascript">
        var str = '123.1a2';
        // 字符串转整型
        console.log(parseInt(str));

        // 字符串转浮点型
        console.log(parseFloat(str));

        // 强制转换
        console.log(Number(str));

        var num = 123.2154;
        // 强转
        console.log(num.toString());
        console.log(String(num));
        // 隐式转换
        console.log('' + num);

        // 保留小数 会四舍五入
        console.log(num.toFixed(3));
    </script>
</body>
</html>